$(".jiaru").on("tap", function() {
	var list = plus.webview.currentWebview("joinreading.html").opener();
	mui.fire(list, 'refresh');
	mui.back()
})

var height = 0
var textnum = 0
var mask = mui.createMask(conceal)
mui('.mui-slider').slider().setStopped(true)
mui.plusReady(function() {
	$("iframe").height($(window).height() - 128)
})
$(".mui-bar-nav").on("tap", function() {
	hide()
})
$(".nav").on("tap", function() {
	hide()
})

function hide() {
	$.each($(".bookshelf"), function(a, e) {
		document.getElementsByClassName('bookshelf')[a].contentWindow.reduction();
		document.getElementsByClassName('bookshelf')[a].contentWindow.mask.close();
	})
}

function conceal() {
	$(".beenadded").removeClass("beenaddedshow")
	$(".bukgg").removeClass("beenaddedshow")
	$(".empty").css("display", "none")
	$('.added_html').css("height", "0px")
}

$(".beenadded").on("tap", function() {
	hide()
	//	$(".shuling").toggle()
	if($(this).hasClass("beenaddedshow")) {

		conceal()
		mask.close()
	} else {
		$(this).addClass("beenaddedshow")
		$(".empty").css("display", "block")
		heght()
		mask.show()

	}
})

$(".bukgg").on("tap", function() {
	hide()
	if($(this).hasClass("beenaddedshow")) {
		conceal()
		mask.close()
	} else {
		$(this).addClass("beenaddedshow")
		$(".empty").css("display", "block")
		heght()
		mask.show()
	}
})

$(".empty").on("tap", function() {
	$(".added_text").remove()
	heght()
	localStorage.removeItem('books')
	//	console.log(document.getElementsByClassName("bookshelf")[0])
	document.getElementsByClassName("bookshelf")[0].contentWindow.add()
	document.getElementsByClassName("bookshelf")[1].contentWindow.add()
})

function heght() {
	height = $(".added_text").eq(0).height() * $(".added_text").length
	$('.added_html').css("height", height)
	texts()
}
texts()

function texts() {
	$(".text_num").text($(".added_text").length + "本")
}

adden()

function adden() {
	var temp = ""
	var books = JSON.parse(localStorage.getItem('books')) || []
	for(var i = 0; i < books.length; i++) {
		temp += '<div class="added_text" bookid=' + books[i].bookid + ' imgsrc=' + books[i].booksrc + '><span>' + books[i].bookname + '</span><div class="remove_url"></div></div>'
	}
	$(".added_html").html(temp)
	texts()

	$(".remove_url").on("tap", function() {
		var addbook = []
		$(this).parent().remove()
		heght()
		var biao = $(".added_html").find(".added_text")
		for(var i = 0; i < biao.length; i++) {
			var obj = {}

			obj.bookid = biao.eq(i).attr("bookid")
			obj.booksrc = biao.eq(i).attr("imgsrc")
			obj.bookname = biao.eq(i).find("span").text()
			addbook.push(obj)
		}

		localStorage.setItem('books', JSON.stringify(addbook))
		document.getElementsByClassName("bookshelf")[0].contentWindow.add()
		document.getElementsByClassName("bookshelf")[1].contentWindow.add()
	})
}

shuaxin()
$(".shuja").on("tap", function() {
	shuaxin()
})

function shuaxin() {
	common.ajax("student/myShelf/queryMyShelf", {
		uid: common.uid,
		token: common.token,
		page: 1,
		pageSize: 1
	}, function(data) {
		if(data.data.list == "") {
			kong()
		} else {
			$(".shuja").css("display", "block")
			$(".emptys").css("display", "none")
		}
	}, "get")
}
$(".seach").on("tap", function() {
	common.open("../teachstudy/seach.html")
})

function parents() {
	var width = $(window).width()
	var length = $(".mui-control-item").length - 1
	var headwidth = $(".mui-control-item").eq(0).width()
	$(".mui-control-item").eq(length).addClass("mui-active").siblings().removeClass("mui-active")
	$(".mui-slider-group").css("transform", "translate3d(-" + length * width + "px, 0px, 0px)")
	$("#sliderProgressBar").css("transform", "translate3d(" + length * headwidth + "px, 0px, 0px)")
}

function kong() {
	$(".emptys").css("display", "block")
	$(".shuja").css("display", "none")
}

$(document).on("tap", ".back", function() {
	mui.back()
})

window.addEventListener('joinreadingggg', function(e) { //执行刷新

	document.getElementsByClassName("bookshelf")[0].contentWindow.shauxin()
	document.getElementsByClassName("bookshelf")[1].contentWindow.shauxin()
	adden()
})